/*
 * @(#) LTException.java  1.0  2004-3-4
 *
 * Copyright 2003 by LongTop Corporation.
 * 2669 XieTu Road, ShangHai, PRC 200030
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * LongTop Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with LongTop.
 */

package com.cnc.bas;

import java.util.ArrayList;
import java.util.List;

/**
 * This is the common superclass for all application exceptions. This
 * class and its subclasses support the chained exception facility that allows
 * a root cause Throwable to be wrapped by this class or one of its
 * descendants. This class also supports multiple exceptions via the
 * exceptionList field.<br>
 * contains a messageKey that can be used as a key in the Struts resource bundle.
 * This key can be passed into the constructor of the ActionError class, and the Struts
 * framework will match it to a message in the Struts resource bundle. This class also
 * contains an object array that the creator of the exception can populate. These objects can
 * then be used to substitute into a message from the bundle that contains substitution
 * parameters based on the MessageFormat class. A message in the bundle might look like this:
 * global.error.invalid.price=The price must be between {0} and {1}.
 */
public class LTException extends Exception {

    private List exceptions = new ArrayList();
    private String messageKey = null;
    private Object[] messageArgs = null;

    public LTException() {
        super();
    }

    public LTException(String key, Object[] args) {
        super();
        this.messageKey = key;
        this.messageArgs = args;
    }

    public LTException(String key) {
        super();
        this.messageKey = key;
        this.messageArgs = null;
    }

    public LTException(Throwable rootCause, String key, Object[] args) {
        super(rootCause);
        this.messageKey = key;
        this.messageArgs = args;
    }

    public LTException(Throwable rootCause) {
        super(rootCause);
    }

    public List getExceptions() {
        return exceptions;
    }

    public void addException(LTException ex) {
        exceptions.add(ex);
    }

    public void setMessageKey(String key) {
        this.messageKey = key;
    }

    public String getMessageKey() {
        return messageKey;
    }

    public void setMessageArgs(Object[] args) {
        this.messageArgs = args;
    }

    public Object[] getMessageArgs() {
        return messageArgs;
    }

    public String getMessage() {
        String myMessage = super.getMessage();
        if (myMessage == null) myMessage = "LTCM Exception - Error Key : " + this.messageKey;
        return myMessage;
    }

    public String[] getMessages() {
        Throwable[] throwables = this.getThrowables();
        String[] msgs = new String[throwables.length];
        for (int i = 0; i < throwables.length; i++) {
            msgs[i] = throwables[i].getMessage();
        }
        return msgs;
    }

    public Throwable[] getThrowables() {
        List list = new ArrayList();
        Throwable throwable = this;
        while (throwable != null) {
            list.add(throwable);
            throwable = throwable.getCause();
        }
        return (Throwable[]) list.toArray(new Throwable[list.size()]);
    }
}
